//
//  ZSProgressView.swift
//  新浪微博-Swift
//
//  Created by 周李波 on 16/7/20.
//  Copyright © 2016年 q. All rights reserved.
//

import UIKit

class ZSProgressView: UIProgressView {
    var timer:NSTimer?
    var isAnim:Bool = true
    
    //MARK:- 初始化
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 64, width: ScreenWidth, height: 2))
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:- 设置UI
    private func setupUI(){
        self.tintColor = UIColor.redColor()
    }
    //MARK:- 开始动画
    func startAnim(){
        self.progress = 0
        self.hidden = false
        timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: #selector(ZSProgressView.valueChanged), userInfo: nil, repeats: true)
        
    }
    //MARK:- 结束动画
    func endAnim(){
        isAnim = false
        
    }
    //MARK:- 移除
    func remove(){
        removeFromSuperview()
    }
    //MARK:- 监听方法
    func valueChanged(){
        if isAnim {
            self.progress += 0.05
            if self.progress > 0.95 {
                self.progress = 0.95
            }
            
        }else{
            if self.progress == 1 {
                self.hidden = true
                timer?.invalidate()
            }else{
                self.progress += 0.05
            }
        }
    }
    //MARK:- 销毁
    deinit{
        timer?.invalidate()
    }

}
